package com.yxb.util;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;

/**
 * @author
 */
public class LocalDateTimeUtil {
	
	
	/**
	 * 格式 yyyy-MM-dd HH:mm:ss
	 */
	public static final DateTimeFormatter DEFAULT_FORMATTER = DateTimeFormatter.ofPattern(
			"yyyy-MM-dd HH:mm:ss");
	
	/**
	 * 格式 yyyy-MM-dd HH:mm
	 */
	public static final DateTimeFormatter DATE_HOUR_MINUTES_FORMATTER = DateTimeFormatter.
			ofPattern("yyyy-MM-dd HH:mm");
	
	/**
	 * 格式 HH:mm
	 */
	public static final DateTimeFormatter HOUR_MINUTES_FORNATTER = DateTimeFormatter.
			ofPattern("HH:mm");
	
	/**
	 * 格式 MM-dd HH:mm
	 */
	public static final DateTimeFormatter NO_YEAR_TIME_FORMATTER = DateTimeFormatter.
			ofPattern("MM-dd HH:mm");
	
	
	/**
	 * 格式 yyyy-MM-dd
	 */
	public static final DateTimeFormatter YEAR_MONTH_DAY_FORMATTER = DateTimeFormatter.
			ofPattern("yyyy-MM-dd");
	
	/**
	 * 格式 HH:mm
	 */
	public static final DateTimeFormatter HOUR_MINUTES_SECOND_FORNATTER = DateTimeFormatter.
			ofPattern("HH:mm:ss");
	
	/**
	 * 格式 MM-dd
	 */
	public static final DateTimeFormatter MONTH_DAY_FORMATTER = DateTimeFormatter.
			ofPattern("MM-dd");
	
	/**
	 * 格式 MM月dd日
	 */
	public static final DateTimeFormatter MONTH_DAY_CHS_FORMATTER = DateTimeFormatter.
			ofPattern("MM月dd日");

	/**
	 * 格式 yyyy年MM月dd日 HH:mm:ss
	 */
	public static final DateTimeFormatter CHINESE_TIME_FORMATTER = DateTimeFormatter.
			ofPattern("yyyy年MM月dd日 HH:mm:ss");

	/**
	 * 格式 MM/dd
	 */
	public static final DateTimeFormatter MMdd = DateTimeFormatter.
			ofPattern("MM/dd");


	/**
	 * 获取LocalDateTime对象对应的epochSecond秒数
	 * @param dateTime
	 * @return
	 */
	public static long toEpochSecond(LocalDateTime dateTime){
		
		return dateTime.toEpochSecond(ZoneOffset.of(
				ZoneOffset.systemDefault().toString()));
	}
	
	/**
	 * 将epochSecond秒数转换成LocalDateTime对象
	 * @param epochSecond
	 * @return
	 */
	public static LocalDateTime getFromEpochSecond(long epochSecond){
		
		return LocalDateTime.ofInstant(Instant
				.ofEpochSecond(epochSecond), 
				ZoneOffset.systemDefault());
	}


	/**
	 * 格式 yyyy年MM月dd日
	 */
	public static final DateTimeFormatter DEFAULT_FORMATTER_CHINESE = DateTimeFormatter.ofPattern(
			"yyyy年MM月dd日");

	/**
	 * 通过epochMillis毫秒数转换为LocalDateTime
	 * @param epochMillis
	 * @return
	 */
	public static LocalDateTime getFromEpochMillis(long epochMillis){
		
		return LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMillis),
				ZoneOffset.systemDefault());
	}


	public static LocalDateTime getFromCalendar(Calendar cal) {
		
		return LocalDateTime.ofInstant(Instant.ofEpochMilli(cal.getTimeInMillis()),
				ZoneOffset.systemDefault());
	}
}
